﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System.Reflection;
using DevExpress.XtraSplashScreen;

namespace Baubixinh_client
{
    public partial class frmExportWholeSales : DevExpress.XtraEditors.XtraForm
    {
        private DataRow mobjRow = null;

        public frmExportWholeSales()
        {
            InitializeComponent();
        }

        private void frmExportWholeSales_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable objStore;
                DataTable objUser;
                //DataTable objSeller;
                DataTable objSanPham;

                objStore = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STORE);
                objUser = clsDbAccess.fncGetUser(Program.gobjDb);
                //objSeller = clsDbAccess.fncGetAttribute(Program.gobjDb, clsEnum.ATTRIBUTE.STAFF);

                objSanPham = new DataTable();
                objSanPham.Columns.Add("product_id");
                objSanPham.Columns.Add("product_name");
                objSanPham.Columns.Add("product_ean");
                objSanPham.Columns.Add("so_luong");
                objSanPham.Columns.Add("don_gia");
                objSanPham.Columns.Add("thanh_tien");
                objSanPham.Columns.Add("color");
                objSanPham.Columns.Add("size");
                objSanPham.Columns.Add("gia_goc");
                objSanPham.Columns.Add("gia_ban_le");
                objSanPham.Columns.Add("gia_cap1");
                objSanPham.Columns.Add("gia_cap2");

                txtStoreFrom.Properties.DataSource = objStore;
                txtStoreTo.Properties.DataSource = objStore;
                repositoryItemLookUpEdit1.DataSource = objStore;
                txtCustomerSelect.Properties.DataSource = objUser;
                //txtSeller.Properties.DataSource = objSeller;
                grdControlProduct.DataSource = objSanPham;

                txtCustomerSelect.Properties.DataSource = objUser;

                txtDate.EditValue = DateTime.Now;
                cbPriceType.SelectedIndex = 0;

                txtStoreFrom.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void rdCustomerType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int intValue = clsCommon.fncCnvNullToInt(rdCustomerType.EditValue);

                if (intValue == 1) cbPriceType.Enabled = true;
                else cbPriceType.Enabled = false;

                //lap lai toan bo grid de set lai gia ban
                xFillGia();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvSearchCustomer_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvSearchCustomer.FocusedRowHandle < 0) return;

                Cursor.Current = Cursors.WaitCursor;

                txtCustomer.Text = clsCommon.fncCnvNullToString(dgvSearchCustomer.GetFocusedRowCellDisplayText(colName));
                txtEmail.Text = clsCommon.fncCnvNullToString(dgvSearchCustomer.GetFocusedRowCellDisplayText(colEmail));
                txtPhone.Text = clsCommon.fncCnvNullToString(dgvSearchCustomer.GetFocusedRowCellDisplayText(colPhone));
                txtStoreTo.EditValue = dgvSearchCustomer.GetFocusedRowCellValue(colKhohang_id);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            try
            {
                if (!validKhoHang.Validate()) return;

                frmChonSp frmChon = new frmChonSp();
                frmChon.evnProductSelected += frmChon_evnProductSelected;
                frmChon.ShowDialog();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void frmChon_evnProductSelected(List<int> lstProductID, List<string> lstProductName, List<string> lstProductEAN, List<double[]> lstGiaBan, List<int> lstNhaSx)
        {
            try
            {
                DataTable objData = (DataTable)grdControlProduct.DataSource;

                for (int i = 0; i <= lstProductID.Count - 1; i++)
                {
                    DataRow objRow = objData.NewRow();
                    objRow["product_id"] = lstProductID[i];
                    objRow["product_name"] = lstProductName[i];
                    objRow["product_ean"] = lstProductEAN[i];
                    objRow["so_luong"] = 1;
                    objRow["gia_goc"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_GOC];
                    objRow["gia_ban_le"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_LE];
                    objRow["gia_cap1"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1];
                    objRow["gia_cap2"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2];

                    if (clsCommon.fncCnvNullToInt(rdCustomerType.EditValue) == 1)
                    {
                        if (cbPriceType.SelectedIndex == 0) objRow["don_gia"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1];
                        else objRow["don_gia"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2];
                    }
                    else
                    {
                        objRow["don_gia"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_LE];
                    }

                    objData.Rows.Add(objRow);
                }
                xTinhTien();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private bool xFillGia()
        {
            try
            {
                int intValue = clsCommon.fncCnvNullToInt(rdCustomerType.EditValue);

                int intSoLuong;
                int intDonGia;
                int intThanhTien;
                int intTotal = 0;

                //lap lai toan bo grid de set lai gia ban
                for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                {
                    if (intValue == 1)
                    {
                        if (cbPriceType.SelectedIndex == 0) intDonGia = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colGia_cap1));
                        else intDonGia = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colGia_cap2));
                    }
                    else
                    {
                        intDonGia = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colGia_le));
                    }

                    intSoLuong = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSo_luong));
                    intThanhTien = intSoLuong * intDonGia;
                    dgvProduct.SetRowCellValue(i, colDon_gia, intDonGia);
                    dgvProduct.SetRowCellValue(i, colThanh_tien, intThanhTien);
                    intTotal += intThanhTien;
                }
                txtTotal.Text = (intTotal + clsCommon.fncCnvNullToInt(txtShipping.Text.Trim())).ToString();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return true;
        }

        private bool xTinhTien()
        {
            try
            {
                int intSoLuong;
                int intDonGia;
                int intThanhTien;
                int intTotal = 0;

                //lap lai toan bo grid de set lai gia ban
                for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                {
                    intSoLuong = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSo_luong));
                    intDonGia = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colDon_gia));
                    intThanhTien = intSoLuong * intDonGia;
                    dgvProduct.SetRowCellValue(i, colThanh_tien, intThanhTien);

                    intTotal += intThanhTien;
                }
                txtTotal.Text = (intTotal + clsCommon.fncCnvNullToInt(txtShipping.Text.Trim())).ToString();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return true;
        }

        private void cbPriceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                xFillGia();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvProduct_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            try
            {
                if (e.RowHandle < 0) return;

                int intProductID = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(e.RowHandle, colProduct_id));
                int intKhoID = clsCommon.fncCnvNullToInt(txtStoreFrom.EditValue);

                if (e.Column == colMau_sac)
                {
                    DataTable objData = clsDbAccess.fncGetColorByProduct(Program.gobjDb, intKhoID, intProductID);

                    RepositoryItemLookUpEdit cbRepository = new RepositoryItemLookUpEdit();
                    cbRepository.DisplayMember = "name";
                    cbRepository.ValueMember = "value";
                    cbRepository.NullText = "Chưa chọn";
                    cbRepository.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("name", "Màu sắc", 75));
                    cbRepository.DataSource = objData;
                    cbRepository.EditValueChanged += cbRepository_EditValueChanged;
                    e.RepositoryItem = cbRepository;
                }

                if (e.Column == colSize)
                {
                    int intMauID = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(e.RowHandle, colMau_sac));
                    if (intMauID <= 0) return;

                    DataTable objData = clsDbAccess.fncGetSizeByProduct(Program.gobjDb, intKhoID, intProductID, intMauID);

                    RepositoryItemLookUpEdit cbRepository = new RepositoryItemLookUpEdit();
                    cbRepository.DisplayMember = "name";
                    cbRepository.ValueMember = "value";
                    cbRepository.NullText = "Chưa chọn";
                    cbRepository.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("name", "Kích thước", 75));
                    cbRepository.DataSource = objData;
                    e.RepositoryItem = cbRepository;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        void cbRepository_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvProduct.FocusedRowHandle < 0) return;
                dgvProduct.SetFocusedRowCellValue(colSize, null);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvProduct_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                if (e.Column == colSo_luong)
                {
                    xTinhTien();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void txtStore_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                //set null for color and size colum
                for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                {
                    dgvProduct.SetRowCellValue(i, colMau_sac, null);
                    dgvProduct.SetRowCellValue(i, colSize, null);
                }

                //load nhan vien
                int intStore = clsCommon.fncCnvNullToInt(txtStoreFrom.EditValue);
                DataTable objSeller = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STAFF, intStore);
                txtSeller1.Properties.DataSource = objSeller;
                txtSeller2.Properties.DataSource = objSeller;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void txtShipping_Leave(object sender, EventArgs e)
        {
            try
            {
                xTinhTien();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                dgvProduct.UpdateCurrentRow();
                //dgvGiaDaiLy.UpdateCurrentRow();

                //tính toán lại lần cuối
                xFillGia();
                xTinhTien();

                //if (!objValidation.Validate()) return;
                if (!validKhoHang.Validate()) return;

                if (!xCheckValid()) return;

                if (dgvProduct.RowCount <= 0)
                {
                    clsCommon.fncMsgWarning("Chưa có sản phẩn nào được chọn.");
                    return;
                }

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu. Bạn có chắc chắn?")) return;

                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                clsDbAccess.stDonHang stDonHang;
                clsDbAccess.stDonHangChiTiet stDonHangChiTiet;
                int intKhoHangMoiID;

                stDonHang = new clsDbAccess.stDonHang();
                stDonHangChiTiet = new clsDbAccess.stDonHangChiTiet();
                intKhoHangMoiID = clsCommon.fncCnvNullToInt(txtStoreTo.EditValue);

                //luu don hang
                //int intLoaiKhach = clsCommon.fncCnvNullToInt(rdCustomerType.EditValue);
                stDonHang.intDon_hang_id = clsDbAccess.fncGetMaxDonHang(Program.gobjDb) + 1;
                stDonHang.intKho_hang_id = clsCommon.fncCnvNullToInt(txtStoreFrom.EditValue);
                stDonHang.intLoai_khach = (int)clsEnum.LOAI_KHACH.CHUYEN_KHO;
                //stDonHang.intLoai_khach = (int)clsEnum.LOAI_KHACH.KHACH_LE;
                //if (intLoaiKhach == 1)
                //{
                //    if (cbPriceType.SelectedIndex == 0) stDonHang.intLoai_khach = (int)clsEnum.LOAI_KHACH.DL_CAP1;
                //    else stDonHang.intLoai_khach = (int)clsEnum.LOAI_KHACH.DL_CAP2;
                //}

                stDonHang.strTen_khach = txtCustomer.Text.Trim();
                stDonHang.strSdt = txtPhone.Text.Trim();
                stDonHang.strEmail = txtEmail.Text.Trim();
                stDonHang.intnguoi_ban1 = clsCommon.fncCnvNullToInt(txtSeller1.EditValue);
                stDonHang.intnguoi_ban2 = clsCommon.fncCnvNullToInt(txtSeller2.EditValue);
                stDonHang.intVan_chuyen = clsCommon.fncCnvNullToInt(txtShipping.Text.Trim());
                stDonHang.intTong_cong = clsCommon.fncCnvNullToInt(txtTotal.Text.Trim());
                stDonHang.strGhi_chu = txtNote.Text.Trim();
                stDonHang.dtNgay_ban = clsCommon.fncCnvNullToDatetime(txtDate.EditValue);

                Program.gobjTrans = Program.gobjConn.BeginTransaction();
                try
                {
                    blnSuccess = blnSuccess && clsDbAccess.fncInsertDonHang(Program.gobjDb, Program.gobjTrans, stDonHang);

                    for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                    {
                        if (!blnSuccess) break;

                        stDonHangChiTiet.intDon_hang_id = stDonHang.intDon_hang_id;
                        stDonHangChiTiet.intProduct_id = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colProduct_id));
                        stDonHangChiTiet.strProduct_name = clsCommon.fncCnvNullToString(dgvProduct.GetRowCellValue(i, colProduct_name));
                        stDonHangChiTiet.intMau_id = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colMau_sac));
                        stDonHangChiTiet.strMu_name = clsCommon.fncCnvNullToString(dgvProduct.GetRowCellDisplayText(i, colMau_sac));

                        stDonHangChiTiet.intSize_id = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSize));
                        stDonHangChiTiet.strSize_name = clsCommon.fncCnvNullToString(dgvProduct.GetRowCellDisplayText(i, colSize));
                        stDonHangChiTiet.intSo_luong = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSo_luong));
                        stDonHangChiTiet.intDon_gia = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colDon_gia));
                        stDonHangChiTiet.intGia_goc = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colGia_goc));

                        blnSuccess = blnSuccess && clsDbAccess.fncInsertDonHangChiTiet(Program.gobjDb, Program.gobjTrans, stDonHangChiTiet);
                        //blnSuccess = blnSuccess && clsDbAccess.fncUpdateDeductProductQuantity(Program.gobjDb, stDonHangChiTiet.intProduct_id, stDonHangChiTiet.intSo_luong);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateDeductProductQuantityDetail(Program.gobjDb, Program.gobjTrans, stDonHangChiTiet.intProduct_id, stDonHang.intKho_hang_id, stDonHangChiTiet.intMau_id, stDonHangChiTiet.intSize_id, stDonHangChiTiet.intSo_luong);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateSoLuongSanPham(Program.gobjDb, Program.gobjTrans, stDonHangChiTiet.intProduct_id, intKhoHangMoiID, stDonHangChiTiet.intMau_id, stDonHangChiTiet.intSize_id, stDonHangChiTiet.intSo_luong);

                    }

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Thêm đơn hàng thành công.");
                    this.Close();
                    Program.gevnApplication.RaiseDonHangUpdate();
                }
                else
                {
                    clsCommon.fncMsgError("Thêm đơn hàng thất bại. Hãy kiểm tra lại dữ liệu.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { SplashScreenManager.CloseForm(false); }
        }

        private void mniCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ không được lưu. Bạn có chắc muốn thoát?")) return;

                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvProduct_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            try
            {
                if (dgvProduct.FocusedColumn != colSo_luong) return;

                int intValue = 0;
                bool blnValid = int.TryParse(clsCommon.fncCnvNullToString(e.Value), out intValue);
                if (!blnValid || intValue <= 0)
                {
                    e.ErrorText = "Giá trị nhập vào không hợp lệ";
                    e.Valid = false;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void txtCustomerSelect_Properties_AddNewValue(object sender, DevExpress.XtraEditors.Controls.AddNewValueEventArgs e)
        {
            try
            {
                frmAddUser frmNewUser = new frmAddUser();
                frmNewUser.ShowDialog();

                if (!frmNewUser.UserAdded) return;

                DataTable objData = (DataTable)txtCustomerSelect.Properties.DataSource;

                if (objData == null) return;

                DataRow objRow = objData.NewRow();
                objRow["id"] = frmNewUser.UserID;
                objRow["username"] = "";
                objRow["name"] = frmNewUser.FullName;
                objRow["email"] = frmNewUser.UserEmail;
                objRow["phone"] = frmNewUser.UserPhone;
                objRow["khohang_id"] = frmNewUser.UserStore;

                objData.Rows.Add(objRow);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnAddSeller_Click(object sender, EventArgs e)
        {
            try
            {
                frmAddSeller frmAdd = new frmAddSeller();
                frmAdd.ShowDialog();

                if (frmAdd.STAFFADDED)
                {
                    int intStore = clsCommon.fncCnvNullToInt(txtStoreFrom.EditValue);
                    if (intStore <= 0) return;
                    DataTable objSeller = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STAFF, intStore);
                    txtSeller1.Properties.DataSource = objSeller;
                    txtSeller2.Properties.DataSource = objSeller;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                //copy row
                mobjRow = dgvProduct.GetFocusedDataRow();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniPaste_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (mobjRow == null) return;

                //paste row
                DataTable objTempTable = (DataTable)grdControlProduct.DataSource;
                objTempTable.Rows.Add(clsCommon.fncCopyDataRow(objTempTable, mobjRow));
                dgvProduct.FocusedRowHandle = dgvProduct.RowCount - 1;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                int intIndex = dgvProduct.FocusedRowHandle;
                if (intIndex < 0) return;

                //xóa row
                if (!clsCommon.fncMsgConfirm("Bản ghi đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                dgvProduct.DeleteSelectedRows();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void dgvProduct_KeyDown(object sender, KeyEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (e.Control && e.KeyCode == Keys.C)
                {
                    //copy row
                    mobjRow = dgvProduct.GetFocusedDataRow();
                }

                if (e.Control && e.KeyCode == Keys.V)
                {
                    if (mobjRow == null) return;

                    //paste row
                    DataTable objTempTable = (DataTable)grdControlProduct.DataSource;
                    objTempTable.Rows.Add(clsCommon.fncCopyDataRow(objTempTable, mobjRow));
                    dgvProduct.FocusedRowHandle = dgvProduct.RowCount - 1;
                }

                if (e.KeyCode == Keys.Delete)
                {
                    int intIndex = dgvProduct.FocusedRowHandle;
                    if (intIndex < 0) return;

                    //xóa row
                    if (!clsCommon.fncMsgConfirm("Dòng đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                    dgvProduct.DeleteSelectedRows();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }


        private bool xCheckValid()
        {
            try
            {
                int intProId;
                int intKho;
                int intMauSac;
                int intSize;
                int intSoLuong;

                intKho = clsCommon.fncCnvNullToInt(txtStoreFrom.EditValue);

                for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                {
                    intMauSac = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colMau_sac));
                    if (intMauSac > 0) continue;

                    clsCommon.fncMsgError("Bạn chưa chọn màu sắc sản phẩm.");
                    dgvProduct.FocusedRowHandle = i;
                    return false;
                }

                for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                {
                    intSize = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSize));
                    if (intSize > 0) continue;

                    clsCommon.fncMsgError("Bạn chưa chọn kích thước sản phẩm.");
                    dgvProduct.FocusedRowHandle = i;
                    return false;
                }

                for (int i = 0; i <= dgvProduct.RowCount - 1; i++)
                {
                    intSoLuong = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSo_luong));
                    if (intSoLuong > 0)
                    {
                        //neu so luong vuot qua so luong trong kho
                        intProId = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colProduct_id));
                        intMauSac = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colMau_sac));
                        intSize = clsCommon.fncCnvNullToInt(dgvProduct.GetRowCellValue(i, colSize));

                        //neu nho hon so luong trong kho thi ok
                        if (intSoLuong <= clsDbAccess.fncGetQuantityByProduct(Program.gobjDb, intKho, intProId, intMauSac, intSize)) continue;

                        //neu lon hon thi thoat
                        clsCommon.fncMsgError("Số lượng nhập vào lớn hơn số lượng hiện có trong kho.");
                        dgvProduct.FocusedRowHandle = i;
                        return false;
                    }

                    clsCommon.fncMsgError("Số lượng sản phẩm phải lớn hơn 0.");
                    dgvProduct.FocusedRowHandle = i;
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message);
                return false;
            }
        }

        private void btnXoaSeller2_Click(object sender, EventArgs e)
        {
            try
            {
                txtSeller2.EditValue = null;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void ucSelectProduct1_evnProductSelected(List<int> lstProductID, List<string> lstProductName, List<string> lstProductEAN, List<double[]> lstGiaBan, List<int> lstNhaSx)
        {
            try
            {
                DataTable objData = (DataTable)grdControlProduct.DataSource;

                for (int i = 0; i <= lstProductID.Count - 1; i++)
                {
                    DataRow objRow = objData.NewRow();
                    objRow["product_id"] = lstProductID[i];
                    objRow["product_name"] = lstProductName[i];
                    objRow["product_ean"] = lstProductEAN[i];
                    objRow["so_luong"] = 1;
                    objRow["gia_goc"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_GOC];
                    objRow["gia_ban_le"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_LE];
                    objRow["gia_cap1"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1];
                    objRow["gia_cap2"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2];

                    if (clsCommon.fncCnvNullToInt(rdCustomerType.EditValue) == 1)
                    {
                        if (cbPriceType.SelectedIndex == 0) objRow["don_gia"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1];
                        else objRow["don_gia"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2];
                    }
                    else
                    {
                        objRow["don_gia"] = lstGiaBan[i][(int)clsEnum.GIA_BAN_INDEX.GIA_LE];
                    }

                    objData.Rows.Add(objRow);
                }
                xTinhTien();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}